const http = require('../../util/http')
Page({
  onShareAppMessage() {
    return {
      title: '首页',
      path: 'entry/index/index',
    };
  },
  data: {
    baseurl: http.baseURL,
    mode: 'aspectFill',
    scrollTop: 0,
    goodsList:[],
    scrollIntoView: 'one',
    scrollTop: 0,
    scrollLeft: 0,
    hasNextPage:true,
    pageNum:1,
  },
  openDetial(e) {
    const item = e.currentTarget.dataset.item
    xhs.navigateTo({
      url: '/entry/orderDetail/index?outTradeNo='+item.outTradeNo
    });
  },
  onShow(){
    this.featchAllOrder()
  },
  featchAllOrder(){
    console.log(this.data.goodsList.length)
    if(!this.data.hasNextPage){
      return
    }
    let parms={
      pageNum:this.data.pageNum
    }
    xhs.showLoading({
      title: '加载中',
    })
    http.get('/api/order/getCouponList',parms).then(res => {
      console.log('res',res.data)
      xhs.hideLoading()
			if (res.code == 200) {
        this.setData({
          goodsList: this.data.goodsList.concat(res.data.list),
          hasNextPage:res.data.hasNextPage
        })
        if(this.data.hasNextPage){
          this.setData({
            pageNum:this.data.pageNum+1,
          })
        }
			}
		})
  },
  creareOrder(e){
    console.log(e)
    const item = e.currentTarget.dataset.item
    let parms ={
      outTradeNo:item.outTradeNo
    }
    http.get('/api/xhs/orderPayment',parms).then(res => {
      console.log('res',res)
			if (res.code == 200) {
        xhs.requestGuaranteeOrderPayment({
          orderInfo: {
            payToken: res.data.pay_token,
            orderId: res.data.order_id,
          },
          success: res => {
            xhs.hideLoading()
            xhs.showToast({
              title: '支付成功'
            })
            console.log('支付结果', res);
          },
          fail: res => {
            xhs.showToast({
              title: '支付失败',
              icon: 'none'
            });
            console.log('支付结果', res);
          },
        });
      } else if(res.code == 902){
        xhs.navigateTo({
          url: "/entry/login/index"
        });
      }
		})

  },
  scroll(e) {
    console.log('获取滚动事件的详细信息e.detail：', e.detail);
    // 此处只做对scrollTop值的监听，不需要使用setData对视图层发起更新
    // this.data.scrollTop = e.detail.scrollTop;
  },
  upper() {
    // this.setData({
    //   hasNextPage:true,
    //   goodsList:[],
    //   pageNum:1
    // })
    // this.featchAllOrder()
  },
  lower() {
    this.featchAllOrder()
    // xhs.showToast({
    //   title: '到底了',
    //   icon: 'none'
    // });
  },
});
